package com.cpi.cornpp.version.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.version.dto.VersionDTO;
import com.cpi.cornpp.version.view.VersionView;

public class VersionConverter {

	private static final String FORMAT_DATE = "MMM yyyy";//$NON-NLS-1$
	
	public VersionView convert(VersionDTO dto){
		VersionView view = null;
		Calendar parStartDate;
		Calendar parEndDate;
		
		if (dto != null){
			view = new VersionView();			
			view.setVersionId(dto.getVersionId());
			SimpleDateFormat startDate = new SimpleDateFormat(FORMAT_DATE);
			view.setStartDate(startDate.format(dto.getStartDate().getTime()).toUpperCase());
			SimpleDateFormat endDate = new SimpleDateFormat(FORMAT_DATE);
			view.setEndDate(endDate.format(dto.getEndDate().getTime()).toUpperCase());
			view.setDescription(dto.getDescription());
			view.setShortName(dto.getShortName());
			view.setParentDescription(dto.getParentDescription());
			view.setParentShortName(dto.getParentShortName());
			SimpleDateFormat parentStartDate = new SimpleDateFormat(FORMAT_DATE);
			parStartDate = dto.getParentStartDate();
			if (parStartDate != null) 
			{
				view.setParentStartDate(parentStartDate.format(parStartDate.getTime()).toUpperCase());
			}
			SimpleDateFormat parentEndDate = new SimpleDateFormat(FORMAT_DATE);
			parEndDate = dto.getParentEndDate();
			if (parEndDate != null) 
			{
				view.setParentEndDate(parentEndDate.format(parEndDate.getTime()).toUpperCase());
			}
			view.setActive(dto.getStatus().booleanValue());
		}
		return view;
		
	}
	
	public VersionDTO convert(VersionView view) throws ParseException{
		VersionDTO dto = null;
		Calendar startDate = null;
		Calendar endDate = null;
		SimpleDateFormat dateFormatter = null;
		
		if (view != null){
			dto = new VersionDTO();
			dateFormatter = new SimpleDateFormat(FORMAT_DATE, new Locale("es"));
			
			dto.setVersionId(view.getVersionId());
			dto.setParentId(view.getParentId());
			dto.setRelatedType(view.getRelatedType());
			dto.setShortName(view.getShortName());
			dto.setDescription(view.getDescription());
			dto.setStatus(Boolean.valueOf(view.isActive()));
			
			if (StringUtils.isNotEmpty(view.getStartDate())){
				startDate = Calendar.getInstance();
				startDate.setTime(dateFormatter.parse(view.getStartDate()));
			}
			
			dto.setStartDate(startDate);
			
			if (StringUtils.isNotEmpty(view.getEndDate())){
				endDate = Calendar.getInstance();
				endDate.setTime(dateFormatter.parse(view.getEndDate()));
			}
			
			dto.setEndDate(endDate);
		}
		
		return dto;
	}
	
	public List convertDTOs(Collection dtoList){
		List resultList = null;
		resultList = new ArrayList(0);
		Iterator iterator = dtoList.iterator();
		VersionDTO dto = null;
		if (!CollectionUtils.isEmpty(dtoList)){
			while( iterator.hasNext() ){
				dto = (VersionDTO)iterator.next();
				resultList.add(convert(dto));
			}
		}
		
		return resultList;
	}
	
}
